package com.pcbly.web.components.taglib;

import java.io.Writer;
import java.util.Locale;
import java.util.Map;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import org.apache.struts2.StrutsStatics;
import org.apache.struts2.components.Component;
import org.apache.struts2.dispatcher.StrutsRequestWrapper;

import com.opensymphony.xwork2.util.ValueStack;
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;

public class Page extends Component {

	private String totalPage;

	private String totalNumber;

	private String curPage;

	private String pageSize;

	private String cssClass;

	private String style;

	private String image;

	private String href;

	private String theme;
	private static ResourceBundle rb = PropertyResourceBundle.getBundle("i18n/messages",
			Locale.getDefault());
	protected final Logger log = LogManager.getLogger(Page.class);

	public Page(ValueStack stack) {
		super(stack);
		// TODO Auto-generated constructor stub
	}

	protected void setStack() {
		/****** read stack start *****/
		getTotalPageStack();
		getTotalNumberStack();
		getCurPageStack();
		getPageSizeStack();

		getCssClassStack();
		getStyleStack();
		getImageStack();
		getHrefStack();
		getThemeStack();
		/****** read stack end *****/
	}

	public boolean start(Writer writer) {
		boolean result = super.start(writer);
		try {
			StringBuilder sb = new StringBuilder("");

			setStack();
			if (totalNumber == null || "0".equals(totalNumber)) {
				sb.append(rb.getString("error.nodata"));
				writer.write(sb.toString());
			} else {
				Map<String, Object> cont = this.getStack().getContext();
				StrutsRequestWrapper request = (StrutsRequestWrapper) cont
						.get(StrutsStatics.HTTP_REQUEST);
				String url = request.getContextPath();
				if (curPage == null) {
					curPage = "1";
				}
				if (totalPage == null) {
					totalPage = "1";
				}
				int icurPage = Integer.parseInt(curPage);
				int itotalPage = Integer.parseInt(totalPage);
				if (null == image) {
					image = url + "/images/icons/";
				}
				if (log.isDebugEnabled()) {
					log.debug("imagePath:::::::" + image);
					log.debug("totalPage:::::::" + totalPage);
					log.debug("totalNumber:::::::" + totalNumber);
					log.debug("curPage:::::::" + curPage);
					log.debug("href:::::::" + href);
					log.debug("url:::::::" + url);
					log.debug("imagePath:::::::" + image);
					log.debug("pageSize:::::::" + pageSize);// href
				}
				sb.append("<table border=\"0\"  cellpadding=\"0\"");
				sb.append("cellspacing=\"0\"  width=\"100%\" ");
				if (!(cssClass == null)) {
					sb.append(" class=\"");
					sb.append(cssClass);
					sb.append("\"");
				}
				if (!(style == null)) {
					sb.append(" style=\"");
					sb.append(style);
					sb.append("\"");
				}
				sb.append(" align=\"right\"  >\r\n");
				sb.append("<tr>\r\n");
				sb.append("<td>&nbsp;</td>\r\n");
				sb.append("<td align=\"right\">");

				/****** write page main start ******/

				if ("1".equals(curPage) && "1".equals(totalPage)) {
					getTotalAndCurPageOne(sb);
				} else if ("1".equals(curPage)) {
					getCurPageOne(sb, icurPage, itotalPage);
					sb.append("<script type=\"text/javascript\">\r\n");
					getScriptNextPage(sb);
					getScriptLastPage(sb);
					sb.append("</script>\r\n");
				} else if ("1".equals(totalPage)) {
					getTotalPageOne(sb, icurPage, itotalPage);
				} else {
					getTotalAndCurPageNotOne(sb, icurPage, itotalPage);
					sb.append("<script type=\"text/javascript\">\r\n");
					getScriptFirstPage(sb);
					getScriptPrePage(sb);
					getScriptNextPage(sb);
					getScriptLastPage(sb);
					sb.append("</script>\r\n");
				}

				/****** write page main end ******/

				sb.append("</tr>\r\n");
				sb.append("</table>\r\n");

				// log.debug("totalPage:::::::" + totalPage);
				// log.debug("totalNumber:::::::" + totalNumber);
				// log.debug("curPage:::::::" + curPage);
				// log.debug("href:::::::" + href);
				// log.debug("pageSize:::::::" + pageSize);// href

				writer.write(sb.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	protected void getTotalAndCurPageOne(StringBuilder sb) {
		sb.append("<img src=\"");
		// sb.append("/web/images/icons/");
		sb.append(image);
		sb.append("/firstPageDisabled.gif\" ");
		// rb

		sb.append("style=\"border:0\"  alt=\"");
		sb.append(rb.getString("page.first"));
		sb.append("\" />");
		sb.append("<img src=\"");
		// sb.append("/web/images/icons/");
		sb.append(image);
		sb.append("/prevPageDisabled.gif\" ");
		sb.append("style=\"border:0\"  alt=\"");
		sb.append(rb.getString("page.pre"));
		sb.append("\" />");
		sb.append("<input style=\"width:10px;ime-mode:active;\" type=\"");
		sb.append("text\" name=\"curPage\" ");
		sb.append(" id=\"curPage\"  ");// disabled=\"true\" 
		sb.append(" value=\"");
		sb.append(curPage);
		sb.append("\" ");
		sb.append("   />");
		sb.append("<img src=\"");
		// sb.append("/web/images/icons/");
		sb.append(image);
		sb.append("/nextPageDisabled.gif\" ");
		//
		sb.append("style=\"border:0\"  alt=\"");
		sb.append(rb.getString("page.next"));
		sb.append("\" />");
		sb.append("<img src=\"");
		// sb.append("/web/images/icons/");
		sb.append(image);
		sb.append("/lastPageDisabled.gif\" ");
		sb.append("style=\"border:0\"  alt=\"");
		sb.append(rb.getString("page.last"));
		sb.append("\" />\r\n");
	}

	protected void getCurPageOne(StringBuilder sb, int icurPage, int itotalPage) {
		sb.append("<img src=\"");
		// sb.append("/web/images/icons/");
		sb.append(image);
		sb.append("/firstPageDisabled.gif\" ");
		sb.append("style=\"border:0\"  alt=\"");
		sb.append(rb.getString("page.first"));
		sb.append("\"/>");
		sb.append("<img src=\"");
		// sb.append("/web/images/icons/");
		sb.append(image);
		sb.append("/prevPageDisabled.gif\" ");
		sb.append("style=\"border:0\"  alt=\"");
		sb.append(rb.getString("page.pre"));
		sb.append("");
		sb.append("\" />");
		sb.append("<input type=\"");
		sb.append("text\" class=\"page_text\"  name=\"curPage\" ");
		sb.append(" id=\"curPage\"  ");
		sb.append(" value=\"");
		sb.append(totalPage);
		sb.append("\" ");
		sb.append("   />");
		sb.append("<img src=\"");
		// sb.append("/web/images/icons/");
		sb.append(image);
		sb.append("/nextPage.gif\" ");
		sb.append("style=\"border:0\"  alt=\"");
		sb.append(rb.getString("page.next"));
		sb.append("\" ");
		sb.append("  onclick=\"javascript:nextPage(");
		sb.append(icurPage + 1 > itotalPage ? itotalPage : icurPage + 1);
		sb.append(");\" />");
		sb.append("<img src=\"");
		// sb.append("/web/images/icons/");
		sb.append(image);
		sb.append("/lastPage.gif\" ");
		sb.append("style=\"border:0\"  alt=\"");
		sb.append(rb.getString("page.last"));
		sb.append("\" ");
		sb.append("  onclick=\"javascript:lastPage(");
		sb.append(itotalPage);
		sb.append(");\" />\r\n");
	}

	protected void getTotalPageOne(StringBuilder sb, int icurPage,
			int itotalPage) {
		sb.append("<img src=\"");
		sb.append(image);
		// sb.append("/web/images/icons/");
		sb.append("/firstPageDisabled.gif\" ");
		sb.append("style=\"border:0\"  alt=\"");
		sb.append(rb.getString("page.first"));
		sb.append("\"/>");
		sb.append("<img src=\"");
		sb.append(image);
		// sb.append("/web/images/icons/");
		sb.append("/prevPageDisabled.gif\" ");
		sb.append("style=\"border:0\"  alt=\"");
		sb.append(rb.getString("page.pre"));
		sb.append("");
		sb.append("\" />");
		sb.append("<input type=\"");
		sb.append("text\"  class=\"page_text\"  name=\"curPage\" ");
		sb.append(" id=\"curPage\" ");
		sb.append(" value=\"");
		sb.append(totalPage);
		sb.append("\" disabled=\"true\" ");
		sb.append("   />");
		sb.append("<img src=\"");
		sb.append(image);
		// sb.append("/web/images/icons/");
		sb.append("/nextPageDisabled.gif\" ");
		sb.append("style=\"border:0\"  alt=\"");
		sb.append(rb.getString("page.next"));
		sb.append("\" ");
		// sb.append("onclick=\"javascript:nextPage(");
		// sb.append(itotalPage);
		// sb.append(");\" />");
		sb.append(" />");
		sb.append("<img src=\"");
		sb.append(image);
		// sb.append("/web/images/icons/");
		sb.append("/lastPageDisabled.gif\" ");
		sb.append("style=\"border:0\"  alt=\"");
		sb.append(rb.getString("page.last"));
		sb.append("\" ");
		// sb.append(" onclick=\"javascript:lastPage(");
		// sb.append(itotalPage);
		// sb.append(");\" />\r\n");
		sb.append(" />\r\n");
	}

	protected void getTotalAndCurPageNotOne(StringBuilder sb, int icurPage,
			int itotalPage) {
		sb.append("<img src=\"");
		// sb.append("/web/images/icons/");
		sb.append(image);
		sb.append("/firstPage.gif\" ");
		sb.append("style=\"border:0\"  alt=\"");
		sb.append(rb.getString("page.first"));
		sb.append("\" ");
		sb.append(" onclick=\"javascript:firstPage(1);\" />");
		sb.append("<img src=\"");
		// sb.append("/web/images/icons/");
		sb.append(image);
		sb.append("/prevPage.gif\" ");
		sb.append("style=\"border:0\"  alt=\"");
		sb.append(rb.getString("page.pre"));
		sb.append("\"  ");
		sb.append("onclick=\"javascript:prePage(");
		sb.append(icurPage - 1);
		sb.append(");\" />");
		sb.append("<input type=\"");
		sb.append("text\"  class=\"page_text\"  name=\"curPage\" ");
		sb.append(" id=\"curPage\" ");
		sb.append(" value=\"");
		sb.append(curPage);
		sb.append("\" ");
		sb.append("   />");
		sb.append("<img src=\"");
		// sb.append("/web/images/icons/");
		sb.append(image);
		sb.append("/nextPage.gif\" ");
		sb.append("style=\"border:0\"  alt=\"");
		sb.append(rb.getString("page.next"));
		sb.append("\"  ");
		sb.append(" onclick=\"javascript:nextPage(");
		sb.append(icurPage + 1 > itotalPage ? itotalPage : icurPage + 1);
		sb.append(");\" />");
		sb.append("<img src=\"");
		// sb.append("/web/images/icons/");
		sb.append(image);
		sb.append("/lastPage.gif\" ");
		sb.append("style=\"border:0\"  alt=\"");
		sb.append(rb.getString("page.last"));
		sb.append("\"  ");
		sb.append("  onclick=\"javascript:lastPage(");
		sb.append(totalPage);
		sb.append(");\" />\r\n");
	}

	protected void getScriptFirstPage(StringBuilder sb) {
		sb.append("function firstPage(arg){ \r\n");
		sb.append("${'curPage'}.value=arg;\r\n");
		sb.append("var form=${'listForm'};\r\n");
		sb.append("form.submit();\r\n");
		sb.append("}\r\n");
	}

	protected void getScriptPrePage(StringBuilder sb) {
		sb.append("function prePage(arg){ \r\n");
		sb.append("${'curPage'}.value=arg;\r\n");
		sb.append("var form=${'listForm'};\r\n");
		sb.append("form.submit();\r\n");
		sb.append("}\r\n");
	}

	protected void getScriptNextPage(StringBuilder sb) {
		sb.append("function nextPage(arg){ \r\n");
		sb.append("${'curPage'}.value=arg;\r\n");
		sb.append("var form=${'listForm'};\r\n");
		sb.append("form.submit();\r\n");
		sb.append("}\r\n");
	}

	protected void getScriptLastPage(StringBuilder sb) {
		sb.append("function lastPage(arg){ \r\n");
		sb.append("${'curPage'}.value=arg;\r\n");
		sb.append("var form=${'listForm'};\r\n");
		sb.append("form.submit();\r\n");
		sb.append("}\r\n");
	}

	protected void getTotalPageStack() {
		if (totalPage != null && totalPage.startsWith("%{")
				&& totalPage.endsWith("}")) {
			totalPage = totalPage.substring(2, totalPage.length() - 1);
			totalPage = String.valueOf(this.getStack().findValue(totalPage));
		} else {
			totalPage = String.valueOf(this.getStack().findValue(totalPage));
		}
	}

	protected void getTotalNumberStack() {
		if (totalNumber != null && totalNumber.startsWith("%{")
				&& totalNumber.endsWith("}")) {
			totalNumber = totalNumber.substring(2, totalNumber.length() - 1);
			totalNumber = String
					.valueOf(this.getStack().findValue(totalNumber));
		} else {
			totalNumber = String
					.valueOf(this.getStack().findValue(totalNumber));
		}
	}

	protected void getCurPageStack() {
		if (curPage != null && curPage.startsWith("%{")
				&& curPage.endsWith("}")) {
			curPage = curPage.substring(2, curPage.length() - 1);
			curPage = String.valueOf(this.getStack().findValue(curPage));
		} else {
			curPage = String.valueOf(this.getStack().findValue(curPage));
		}
	}

	protected void getPageSizeStack() {
		if (pageSize != null && pageSize.startsWith("%{")
				&& pageSize.endsWith("}")) {
			pageSize = pageSize.substring(2, pageSize.length() - 1);
			pageSize = String.valueOf(this.getStack().findValue(pageSize));
		} else {
			pageSize = String.valueOf(this.getStack().findValue(pageSize));
		}
	}

	protected void getCssClassStack() {
		if (cssClass != null && cssClass.startsWith("%{")
				&& cssClass.endsWith("}")) {
			cssClass = cssClass.substring(2, cssClass.length() - 1);
		}
	}

	protected void getStyleStack() {
		if (style != null && style.startsWith("%{") && style.endsWith("}")) {
			style = style.substring(2, style.length() - 1);
		}
	}

	protected void getImageStack() {
		if (image != null && image.startsWith("%{") && image.endsWith("}")) {
			image = image.substring(2, image.length() - 1);
			log.debug("aaaaaaaaaaaaaaaaaaaa     image  ");
		}
	}

	protected void getHrefStack() {
		if (href != null && href.startsWith("%{") && href.endsWith("}")) {
			href = href.substring(2, href.length() - 1);
		}
	}

	protected void getThemeStack() {
		if (theme != null && theme.startsWith("%{") && theme.endsWith("}")) {
			theme = theme.substring(2, theme.length() - 1);
			theme = String.valueOf(this.getStack().findValue(theme));
		}
	}

	public String getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(String totalPage) {
		this.totalPage = totalPage;
	}

	public String getTotalNumber() {
		return totalNumber;
	}

	public void setTotalNumber(String totalNumber) {
		this.totalNumber = totalNumber;
	}

	public String getCurPage() {
		return curPage;
	}

	public void setCurPage(String curPage) {
		this.curPage = curPage;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	public String getTheme() {
		return theme;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
